package com.example.demo.window;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ch.selise.exercise.pattern.notepad.storage.AppStorageSingleton;

import com.example.demo.command.AddActionCommand;
import com.example.demo.command.DeleteActionCommand;
import com.example.demo.command.EditActionCommand;
import com.example.demo.command.MenuCommand;
import com.example.demo.views.DataEntryTable;
import com.example.demo.views.DataEntryTable.ColumnProperty;
import com.example.demo.views.DataEntryTable.DataEntryTableDelegate;
import com.vaadin.data.Property;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class DemoWindow extends Window{

	private static final long serialVersionUID = -6554247836418005890L;
	

	public DemoWindow() {
		super();
	}
	
	public DemoWindow(String string) {
		this();
		setCaption(string);
	}

	@Override
	public void attach() {
		
		super.attach();
		
		//Setup User Interface
		setupUI();
	}

	private void setupUI() {
		
		VerticalLayout rootLayout = new VerticalLayout();
		rootLayout.setSizeUndefined();
		setContent(rootLayout);
		
		addComponent(getTopToolBar());
		addComponent(getContentPanel());

	}

	private Panel contentPanel = null;
			
	private Component getContentPanel() {
		
		if(this.contentPanel == null){
			this.contentPanel = new Panel("");
			this.contentPanel.setSizeUndefined();
			
			HorizontalLayout hL = new HorizontalLayout();
			hL.setSizeUndefined();
			this.contentPanel.addComponent(hL);
			
			hL.addComponent(getInputPromt());
			hL.addComponent(getAddButton());
						
			//Loading data if refresh.
			List<Map<String, Object>> dataSet = new ArrayList<Map<String,Object>>();		
			for (String entry : AppStorageSingleton.getInstance().getText()) {
				Map<String, Object> x = new HashMap<String, Object>();
				x.put("Text", entry);
				x.put("Edit", "Edit");
				x.put("Delete", "Del");
				dataSet.add(x);
			}
			getTableX().loadOrderdData(dataSet, false);
			//
			this.contentPanel.addComponent(getTableX());
			
		}
		return this.contentPanel;
	}
	
	private DataEntryTable tableX = null;
	
	public DataEntryTable getTableX() {
		
		if(tableX == null){
			
			List<ColumnProperty> propertiesX = new ArrayList<DataEntryTable.ColumnProperty>();
			ColumnProperty xt = new ColumnProperty("Text", TextField.class, "Text");
			xt.setColumnWidth(300);
			xt.setExpandRatio(3.0f);
			propertiesX.add(xt);
			propertiesX.add(new ColumnProperty("Edit", Button.class, ""));
			propertiesX.add(new ColumnProperty("Delete", Button.class, ""));
			tableX = new DataEntryTable(propertiesX);
			tableX.setWidth("450px");
			tableX.setPageLength(20);
			tableX.setDelegate(new DataEntryTableDelegate() {
				
				@Override
				public void onColumnFieldCreate(Object propertyId, Class<?> type,
						Field field) {
					
					if(propertyId.equals("Edit")){
						if(field instanceof Button){
							((Button) field).addListener(new EditActionCommand("edit", getTableX()));
						}
					}
					else if(propertyId.equals("Delete")){
						if(field instanceof Button){
							((Button) field).addListener(new DeleteActionCommand("delete",getTableX()));
						}
					}else if(propertyId.equals("Text")){
						((TextField)field).setReadOnly(true);
					}
				}
				
				@Override
				public String formatTablePropertyValue(Object rowId, Object colId,
						Property property) {
					// TODO Auto-generated method stub
					return null;
				}
			});
		}
		return tableX;
	}

	public void setSelect(DataEntryTable select) {
		this.tableX = select;
	}



	private Property buffer = null;
	
	public Property getBuffer() {
		return buffer;
	}

	public void setBuffer(Property buffer) {
		this.buffer = buffer;
	}

	private TextField inputPromt = null;
	
	public TextField getInputPromt() {
		
		if(inputPromt == null){
			inputPromt = new TextField();
			inputPromt.setInputPrompt("Enter Text");
			inputPromt.setWidth("400px");
		}
		return inputPromt;
	}

	public void setInputPromt(TextField inputPromt) {
		this.inputPromt = inputPromt;
	}

	private Button addButton = null;

	public Button getAddButton() {
		
		if(addButton == null){
			Button add = new Button("Add");
			add.setImmediate(true);
			add.addListener(new AddActionCommand("add",getTableX(),getInputPromt()));
			addButton = add;
		}
		return addButton;
	}

	public void setAddButton(Button addButton) {
		this.addButton = addButton;
	}

	private MenuBar topToolBar = null;

	public MenuBar getTopToolBar() {
		if(topToolBar == null){
			topToolBar = new MenuBar();
			
			MenuCommand command = new MenuCommand("undo",getTableX());
			topToolBar.addItem("Undo", command);
			
			command = new MenuCommand("redo",getTableX());
			topToolBar.addItem("Redo", command);
		}
		return topToolBar;
	}
}
